package com.kaichaohulian.baocms.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.adapter.NearbyListAdapter;
import com.kaichaohulian.baocms.app.MyApplication;
import com.kaichaohulian.baocms.entity.NearbyBean;
import com.kaichaohulian.baocms.http.HttpArray;
import com.kaichaohulian.baocms.retrofit.RetrofitClient;
import com.kaichaohulian.baocms.rxjava.BaseListObserver;
import com.kaichaohulian.baocms.rxjava.RxUtils;
import com.kaichaohulian.baocms.util.TitleUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2015/12/11.
 */
public class TouziFragment extends Fragment implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.rv_nearby_list)
    RecyclerView rvNearbyList;
    @BindView(R.id.refresh_nearby)
    SwipeRefreshLayout refreshNearby;
    @BindView(R.id.rl_near_list)
    LinearLayout rlNearList;
    Unbinder unbinder;
    private NearbyListAdapter mAdapter;
    private List<NearbyBean> mList;
    private TextView tvGirl, tvBoy, tvAll, tvGreet, tvClear, tvCancel;
    private LinearLayout popLayout;
    private PopupWindow popupWindow;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//        View view = inflater.inflate(R.layout.activity_nearby_list, null);
        View view = inflater.inflate(R.layout.fragment_secone, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initEvent();
        onRefresh();
        return view;
    }

    public void initView() {
        try {
            new TitleUtils(getActivity()).setTitle("附近的人")
                    .showRight()
                    .setRightListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            openBottomPopWindow();
                        }
                    });
            mList = new ArrayList<>();
            mAdapter = new NearbyListAdapter(R.layout.item_neayby_list, mList);
            rvNearbyList.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvNearbyList.setAdapter(mAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void openBottomPopWindow() {
        try {
            View view = View.inflate(getActivity(), R.layout.pop_nearby_list, null);
            popLayout = (LinearLayout) view.findViewById(R.id.ll_pop_near_out);
            tvGirl = (TextView) view.findViewById(R.id.ll_pop_near_girl);
            tvBoy = (TextView) view.findViewById(R.id.ll_pop_near_boy);
            tvAll = (TextView) view.findViewById(R.id.ll_pop_near_all);
            tvGreet = (TextView) view.findViewById(R.id.ll_pop_near_greet);
            tvClear = (TextView) view.findViewById(R.id.ll_pop_near_clear);
            tvCancel = (TextView) view.findViewById(R.id.ll_pop_near_cancel);

            popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            popupWindow.setTouchable(true);
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAtLocation(rlNearList, Gravity.BOTTOM, 0, 0);

            popLayout.setOnClickListener(this);
            tvAll.setOnClickListener(this);
            tvBoy.setOnClickListener(this);
            tvCancel.setOnClickListener(this);
            tvClear.setOnClickListener(this);
            tvGirl.setOnClickListener(this);
            tvGreet.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void initEvent() {
        try {
            getNearUser(1, 2);
            mAdapter.setOnItemClickListener(this);
            refreshNearby.setOnRefreshListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({  R.id.rv_nearby_list, R.id.refresh_nearby, R.id.rl_near_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.rv_nearby_list:
                break;
            case R.id.refresh_nearby:
                break;
            case R.id.rl_near_list:
                break;
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh() {
        try {

            if (TextUtils.isEmpty( MyApplication.getInstance().UserInfo.getUserId()+"")){

            }else {
                getNearUser(1, 2);
            }
        }catch (Exception e){

        }




    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    private void getNearUser(int page, int sex) {
        Map<String, String> map = new HashMap<>();
        map.put("id", MyApplication.getInstance().UserInfo.getUserId() + "");
        map.put("userId", MyApplication.getInstance().UserInfo.getUserId() + "");
        map.put("longitud", MyApplication.lng);
        map.put("latitude", MyApplication.lat);
        map.put("page", page + "");
        map.put("sex", sex + "");//0 男 1女 2全部
        RetrofitClient.getInstance().createApi().getNearUser(map)
                .compose(RxUtils.<HttpArray<NearbyBean>>io_main())
                .subscribe(new BaseListObserver<NearbyBean>(getActivity(), "加载中", refreshNearby) {
                    @Override
                    protected void onHandleSuccess(List<NearbyBean> list) {
                        for (NearbyBean nearbyBean : list) {
                            Log.d("NearbyListActivity", nearbyBean.toString());
                        }
                        mList.clear();
                        mList.addAll(list);
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }
}
